package com.huawei.java.main.io;

import java.io.*;
import java.util.List;

/**
 * @author dengguoli
 * @date 2022/3/17
 * @since jdk1.8
 */
public class Writer {

    /**使用FileOutputStream来写入txt文件
     */
    public static void writeTxt(List<String> text) {
        String txtPath = "D:\\CodePro\\idea\\codecraft2022\\data\\test\\solution.txt";
        FileOutputStream fileOutputStream = null;
        File file = new File(txtPath);
        try {
            if(file.exists()){
                //判断文件是否存在，如果不存在就新建一个txt
                boolean isNewFile = file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            for (String s : text) {
                fileOutputStream.write(s.getBytes());
            }
            fileOutputStream.flush();
            fileOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
